
/*
 *  Based on GCC compiler and modified http://gcc.gnu.org
 *  A splay-tree datatype.
 *  Copyright (C) 1998, 1999, 2000, 2001, 2009,
 *  2010, 2011 Free Software Foundation, Inc.
 *
 *  This file is part of GNU CC. gcc-4.8 december 2012
 *
 *  GNU CC is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  GNU CC is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU CC; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 51 Franklin Street - Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 *  These are the four essential freedoms with GNU GPL software:
 *  1: freedom to run the program, for any purpose
 *  2: freedom to study how the program works, and change it to make it do what you wish
 *  3: freedom to redistribute copies to help your Free Software girlfriends and friends
 *  4: freedom to distribute copies of your modified versions to your Free Software girlfriends and friends
 *   ,           ,
 *  /             \
 * ((__-^^-,-^^-__))
 * `-_---'  `---_-'
 *  `--|o`   'o|--'
 *      \  `  /
 *       ): :(
 *       :o_o:
 *        "-"
 */


#include "config.h"


#include <stdlib.h>


#include <stdio.h>
#include <string.h>

#include "main.h"
#include "splay-tree.h"

/* */
#ifdef __APPLE__
#error ""
#endif

/* */
#if defined __cplusplus
#error ""
#endif

/* */
#if defined(_MSC_VER)
#error ""
#endif

#define mywrapped_malloc malloc
#define mywrapped_free free

/* */
static void splay_tree_delete_helper (splay_tree, splay_tree_node);

/* */
static inline void rotate_left (splay_tree_node *, splay_tree_node, splay_tree_node);

/* */
static inline void rotate_right (splay_tree_node *, splay_tree_node, splay_tree_node);

/* */
static void splay_tree_splay (splay_tree, splay_tree_key);

/* Deallocate NODE (a member of SP), and all its sub-trees.  */
static void
splay_tree_delete_helper (splay_tree sp, splay_tree_node node)
{
  splay_tree_node pending = (splay_tree_node)0;
  splay_tree_node active = (splay_tree_node)0;

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return;
  }

  /* nodata */
  if (node == (splay_tree_node)0) {
    return;
  }

#define KDEL(x)  if (sp->delete_key) { (*sp->delete_key)(x); }
#define VDEL(x)  if (sp->delete_value) { (*sp->delete_value)(x); }

  KDEL (node->key); node->key = (splay_tree_key)0;
  VDEL (node->value); node->value = (splay_tree_value)0;

  /* We use the "key" field to hold the "next" pointer.  */
  node->key = (splay_tree_key)pending;
  pending = (splay_tree_node)node;

  /* Now, keep processing the pending list until there aren't any
     more.  This is a little more complicated than just recursing, but
     it doesn't toast the stack for large trees.  */

  while (pending)
    {

      active = pending;

      pending = (splay_tree_node)0;

      while (active)
	{
	   splay_tree_node temp = (splay_tree_node)0;

	  /* active points to a node which has its key and value
	     deallocated, we just need to process left and right.  */

	  if (active->left)
	    {
	      KDEL (active->left->key);
	      VDEL (active->left->value);
	      active->left->key = (splay_tree_key)pending;
	      pending = (splay_tree_node)(active->left);
	    }
	  if (active->right)
	    {
	      KDEL (active->right->key);
	      VDEL (active->right->value);
	      active->right->key = (splay_tree_key)pending;
	      pending = (splay_tree_node)(active->right);
	    }

	  /* current */
	  temp = active;

	  /* set next to run on */
	  active = (splay_tree_node)(temp->key);

	  /* wipe current active */
	  (*sp->deallocate) ((void *) temp, (void *) sp->allocate_data);
	}
    }

#undef KDEL
#undef VDEL

  return;
}

/* Rotate the edge joining the left child N with its parent P.  PP is the
   grandparents' pointer to P.  */

static inline void
rotate_left (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
{
  splay_tree_node tmp = (splay_tree_node)0;
  tmp = n->right;
  n->right = p;
  p->left = tmp;
  *pp = n;
  return;
}

/* Rotate the edge joining the right child N with its parent P.  PP is the
   grandparents' pointer to P.  */

static inline void
rotate_right (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
{
  splay_tree_node tmp = (splay_tree_node)0;
  tmp = n->left;
  n->left = p;
  p->right = tmp;
  *pp = n;
  return;
}

/* Bottom up splay of key.  */

static void
splay_tree_splay (splay_tree sp, splay_tree_key key)
{

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
     return;
  }

  /* no data */
  if (sp->root == (splay_tree_node)0) {
    return;
  }

  do
  {
    int cmp1 = 0;
    int cmp2 = 0;
    splay_tree_node n = (splay_tree_node)0;
    splay_tree_node c = (splay_tree_node)0;

    n = sp->root;

    if (*sp->compvalue) {
      cmp1 = (*sp->compvalue) (key, n->value);
    } else {
      cmp1 = (*sp->comp) (key, n->key);
    }

    /* Found.  */
    if (cmp1 == 0) {
      return;
    }

    /* Left or right?  If no child, then we're done.  */
    if (cmp1 < 0) {
      c = n->left;
    } else {
      c = n->right;
    }

    if (c == (splay_tree_node)0) {
      return;
    }

    /* Next one left or right?  If found or no child, we're done
       after one rotation.  */
    if (*sp->compvalue) {
      cmp2 = (*sp->compvalue) (key, c->value);
    } else {
      cmp2 = (*sp->comp) (key, c->key);
    }

    if (
           (cmp2 == 0)
        || ((cmp2 < 0) && (c->left == (splay_tree_node)0))
        || ((cmp2 > 0) && (c->right == (splay_tree_node)0))
        )
      {
	if (cmp1 < 0) {
	  rotate_left (&sp->root, n, c);
	} else {
	  rotate_right (&sp->root, n, c);
        }
        return;
      }

    /* Now we have the four cases of double-rotation.  */
    if ((cmp1 < 0) && (cmp2 < 0))
      {
	rotate_left (&n->left, c, c->left);
	rotate_left (&sp->root, n, n->left);
      }
    else if ((cmp1 > 0) && (cmp2 > 0))
      {
	rotate_right (&n->right, c, c->right);
	rotate_right (&sp->root, n, n->right);
      }
    else if ((cmp1 < 0) && (cmp2 > 0))
      {
	rotate_right (&n->left, c, c->right);
	rotate_left (&sp->root, n, n->left);
      }
    else if ((cmp1 > 0) && (cmp2 < 0))
      {
	rotate_left (&n->right, c, c->left);
	rotate_right (&sp->root, n, n->right);
      }
    else
      {
      /* actually generates assembly sourcecode. */
      }
  } while (1);

  return;
}


/* */
static void *
splay_tree_xmalloc_allocate (size_t size, void *data)
{
  /* pacify GCC */
  if (data) {}
  return (void *) mywrapped_malloc(size);
}

/* */
static void
splay_tree_xmalloc_deallocate (void *object, void *data)
{
  /* pacify GCC */
  if (data) {}
  (void) mywrapped_free (object);
  return;
}


/* Allocate a new splay tree, using COMPARE_FN to compare nodes,
   DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
   values.  Use xmalloc to allocate the splay tree structure, and any
   nodes added.  */

splay_tree 
splay_tree_new (splay_tree_compare_fn compare_fn,
                splay_tree_delete_key_fn delete_key_fn,
                splay_tree_delete_value_fn delete_value_fn)
{
  return (splay_tree_new_with_allocator
          (compare_fn, delete_key_fn, delete_value_fn,
           splay_tree_xmalloc_allocate, splay_tree_xmalloc_deallocate, (void *)0));
}


/* Allocate a new splay tree, using COMPARE_FN to compare nodes,
   DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
   values.  */

splay_tree 
splay_tree_new_with_allocator (splay_tree_compare_fn compare_fn,
                               splay_tree_delete_key_fn delete_key_fn,
                               splay_tree_delete_value_fn delete_value_fn,
                               splay_tree_allocate_fn allocate_fn,
                               splay_tree_deallocate_fn deallocate_fn,
                               void *allocate_data)
{
  return
    splay_tree_new_typed_alloc (compare_fn, delete_key_fn, delete_value_fn,
				allocate_fn, allocate_fn, deallocate_fn,
				allocate_data);
}

splay_tree
splay_tree_new_typed_alloc (splay_tree_compare_fn compare_fn,
			    splay_tree_delete_key_fn delete_key_fn,
			    splay_tree_delete_value_fn delete_value_fn,
			    splay_tree_allocate_fn tree_allocate_fn,
			    splay_tree_allocate_fn node_allocate_fn,
			    splay_tree_deallocate_fn deallocate_fn,
			    void * allocate_data)
{
  splay_tree sp = (splay_tree)0;

  sp = (splay_tree) (*tree_allocate_fn)
    (sizeof (struct splay_tree_s), allocate_data);

  if (sp == (splay_tree)0) {
    return ((splay_tree)0);
  }

  /* The root of the tree.  */
  sp->root = (splay_tree_node)0;

  /* The compare function on the key data. */
  sp->comp = (splay_tree_compare_fn)compare_fn;

  /* The compare function on the value data. */
  sp->compvalue = (splay_tree_compare_fn)0;

  /* The deallocate-key function.  NULL if no cleanup is necessary.  */
  sp->delete_key = delete_key_fn;

  /* The deallocate-value function.  NULL if no cleanup is necessary.  */
  sp->delete_value = delete_value_fn;

  /* Node allocate function.  Takes allocate_data as a parameter. */
  sp->allocate = node_allocate_fn;

  /* Free function for nodes and trees.  Takes allocate_data as a parameter.  */
  sp->deallocate = deallocate_fn;

  /* Parameter for allocate/free functions.  */
  sp->allocate_data = allocate_data;

  /* number of data entries */
  sp->splay_tree_entries = 0;

  /* oke */
  return ((splay_tree)sp);
}

/* Deallocate SP.  */

void
splay_tree_delete (splay_tree sp)
{

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return;
  }

  /* nodata */
  if (sp->root) {
    (void) splay_tree_delete_helper (sp, sp->root);
  }

  /* */
  if ((*sp->deallocate)) {
    (*sp->deallocate)((void *)sp, (void *)sp->allocate_data);
  }

  return;
}

/* similar splay_tree_delete() but keep splay itself intact */
void
splay_tree_empty (splay_tree sp)
{
  splay_tree_node nmin = (splay_tree_node)0;
  splay_tree_node nmax = (splay_tree_node)0;

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return;
  }

  nmin = splay_tree_min (sp);

  if (nmin == (splay_tree_node)0) {
    return;
  }

  nmax = splay_tree_min (sp);

  if (nmax == (splay_tree_node)0) {
    return;
  }

  /* fixup  */

  return;
}

/* how many splay_tree_node entries are in splay_tree sp */
size_t splay_tree_entries (splay_tree sp)
{

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return (0);
  }

  /* no data */
  if (sp->root == (splay_tree_node)0) {
    return (0);
  }

  /* number of data entries */
  return (sp->splay_tree_entries);
}

/* Insert a new node (associating KEY with DATA) into SP.  If a
   previous node with the indicated KEY exists, its data is replaced
   with the new value.  Returns the new node.  */

splay_tree_node
splay_tree_insert (splay_tree sp, splay_tree_key key, splay_tree_value value)
{
  int comparison = 0;

  if (sp == (splay_tree)0) {
    return ((splay_tree_node)0);
  }

  /* search database */
  (void) splay_tree_splay (sp, key);

  if (sp == (splay_tree)0) {
    return ((splay_tree_node)0);
  }

  /* if any data */
  if (sp->root) {
    if (*sp->compvalue) {
      comparison = (*sp->compvalue)(sp->root->value, key);
    } else {
      comparison = (*sp->comp)(sp->root->key, key);
    }
  }

  if (sp->root && comparison == 0)
    {

      /* If the root of the tree already has the indicated KEY, just
	 replace the value with VALUE.  */
      if (sp->delete_value) {
	(void)(*sp->delete_value)(sp->root->value);
      }

      sp->root->value = value;
    }
  else
    {
      /* Create a new node, and insert it at the root.  */
      splay_tree_node node = (splay_tree_node)0;

      node = ((splay_tree_node) (*sp->allocate) (sizeof (struct splay_tree_node_s), sp->allocate_data));

      if (node == (splay_tree_node)0) {
        return ((splay_tree_node)0);
      }

      node->key = key;
      node->value = value;

      /* number of data entries */
      sp->splay_tree_entries = (sp->splay_tree_entries + 1);

      /* only 1st time */
      if (sp->root == (splay_tree_node)0)
        {
	  node->left = (splay_tree_node)0;
	  node->right = (splay_tree_node)0;
        }
      else if (comparison < 0)
	{
	  node->left = sp->root;
	  node->right = node->left->right;
	  node->left->right = (splay_tree_node)0;
	}
      else
	{
	  node->right = sp->root;
	  node->left = node->right->left;
	  node->right->left = (splay_tree_node)0;
	}

      sp->root = node;
    }

  return ((splay_tree_node)sp->root);
}

/* Remove KEY from SP.  It is not an error if it did not exist.  */

void
splay_tree_remove (splay_tree sp, splay_tree_key key)
{
  splay_tree_node left = (splay_tree_node)0;
  splay_tree_node right = (splay_tree_node)0;
  int compare = 0;

  /* if no splay root */
  if ((splay_tree)0 == sp) {
    return;
  }

  /* if no data */
  if (sp->root == (splay_tree_node)0) {
    return;
  }

  (void) splay_tree_splay (sp, key);

  if (*sp->compvalue) {
   compare = (*sp->compvalue) (sp->root->value, key);
  } else {
   compare = (*sp->comp) (sp->root->key, key);
  }

  /* unfound */
  if (compare) {
    return;
  }

  left = sp->root->left;
  right = sp->root->right;

  /* Delete the root node itself.  */
  if (sp->delete_value) {
    (void)(*sp->delete_value) (sp->root->value);
  }

  /* */
  (void) (*sp->deallocate) ((void *) sp->root, (void *) sp->allocate_data);

  /* number of data entries */
  if (sp->splay_tree_entries > 0) {
    sp->splay_tree_entries = (sp->splay_tree_entries - 1);
  } else {
    /* shouldnothappen */
    (void) fputs ("splay_tree_remove_should_not_happen_fixup",stderr);
    (void) fflush (stderr);
    sp->splay_tree_entries = 0;
  }

  /* One of the children is now the root.  Doesn't matter much
   * which, so long as we preserve the properties of the tree. 
   */
  if (left) {

    sp->root = left;

    /* If there was a right child as well, hang it off the 
      right-most leaf of the left child.  */
    if (right) {

      while (left->right)
      {
        left = left->right;
      }

      left->right = right;
    }

  } else {
    sp->root = right;
  }

  return;
}

/* Lookup KEY in SP, returning VALUE if present, and NULL 
   otherwise.  */

splay_tree_node
splay_tree_lookup (splay_tree sp, splay_tree_key key)
{
  int compare = 0;

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return ((splay_tree_node)0);
  }

  /* no data */
  if (sp->root == (splay_tree_node)0) {
    return ((splay_tree_node)0);
  }

  /* */
  (void) splay_tree_splay (sp, key);

  /* */
  if (*sp->compvalue) {
    compare = (*sp->compvalue)(sp->root->value, key);
  } else {
    compare = (*sp->comp)(sp->root->key, key);
  }

  /* */
  if (compare == 0) {
    return ((splay_tree_node)sp->root);
  } else {
    return ((splay_tree_node)0);
  }
}

/* search by value. the compare function switching causes extra rebalancing.  */
splay_tree_node splay_tree_lookup_value (splay_tree sp, splay_tree_key key, splay_tree_compare_fn compare)
{
  splay_tree_node n = (splay_tree_node)0;

  if (compare == (splay_tree_compare_fn)0) {
    return ((splay_tree_node)0);
  }

  /* <nil> splaytree */
  if (sp == (splay_tree)0) {
    return ((splay_tree_node)0);
  }

  /* no data */
  if (sp->root == (splay_tree_node)0) {
    return ((splay_tree_node)0);
  }

  /* The comparision function on the value data. */
  sp->compvalue = (splay_tree_compare_fn)compare;

  /* search */
  n = splay_tree_lookup (sp,key);

  /* reset the compare function */
  sp->compvalue = (splay_tree_compare_fn)0;

  /* */
  return ((splay_tree_node)n);
}

/* Return the node in SP with the greatest key.  */
splay_tree_node splay_tree_max (splay_tree sp)
{
  splay_tree_node n = (splay_tree_node)0;

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return ((splay_tree_node)0);
  }

  n = sp->root;

  /* no data */
  if (n == (splay_tree_node)0) {
    return ((splay_tree_node)0);
  }

  /* scan r */
  while (n->right)
    {
      n = n->right;
    }

  return ((splay_tree_node)n);
}

/* Return the node in SP with the smallest key.  */

splay_tree_node
splay_tree_min (splay_tree sp)
{
  splay_tree_node n = (splay_tree_node)0;

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return ((splay_tree_node)0);
  }

  n = sp->root;

  /* no data */
  if ((splay_tree_node)0 == n) {
    return ((splay_tree_node)0);
  }

  /* scan l */
  while (n->left)
    {
      n = n->left;
    }

  return ((splay_tree_node)n);
}

/* return node at current splay tree root if it has same key or 0 if not */
splay_tree_node
splay_tree_current (splay_tree sp, splay_tree_key key)
{
  int comparison = 0;

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return ((splay_tree_node)0);
  }

  /* no data */
  if (sp->root == (splay_tree_node)0) {
    return ((splay_tree_node)0);
  }

  /* */
  if (*sp->compvalue) {
    comparison = (*sp->compvalue)(sp->root->value, key);
  } else {
    comparison = (*sp->comp)(sp->root->key, key);
  }

  /* 0 if equal then return new root */
  if (comparison == 0) {
    return ((splay_tree_node)sp->root);
  } else {
    return ((splay_tree_node)0);
  }
}

/* Return the immediate predecessor KEY, or NULL if there is no
   predecessor.  KEY need not be present in the tree.  */

splay_tree_node
splay_tree_predecessor (splay_tree sp, splay_tree_key key)
{
  splay_tree_node node = (splay_tree_node)0;
  int comparison = 0;

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return ((splay_tree_node)0);
  }

  /* no data */
  if (sp->root == (splay_tree_node)0) {
    return ((splay_tree_node)0);
  }

  /* Splay the tree around KEY.  That will leave either the KEY
     itself, its predecessor, or its successor at the root.  */
  (void) splay_tree_splay (sp, key);

  /* */
  if (*sp->compvalue) {
    comparison = (*sp->compvalue)(sp->root->value, key);
  } else {
    comparison = (*sp->comp)(sp->root->key, key);
  }

  /* If the predecessor is at the root, just return it.  */
  if (comparison < 0) {
    return ((splay_tree_node)sp->root);
  }

  /* Otherwise, find the rightmost element of the left subtree.  */
  node = sp->root->left;

  if (node) {
    /* scan r */
    while (node->right)
    {
      node = node->right;
    }
  }

  return ((splay_tree_node)node);
}

/* Return the immediate successor KEY, or NULL if there is no
   successor.  KEY need not be present in the tree. fixup: not true */

splay_tree_node
splay_tree_successor (splay_tree sp, splay_tree_key key)
{
  splay_tree_node node = (splay_tree_node)0;
  int comparison = 0;

  /* <nil> splaytree */
  if (sp == (splay_tree)0) {
    return ((splay_tree_node)0);
  }

  /* no data */
  if (sp->root == (splay_tree_node)0) {
    return ((splay_tree_node)0);
  }

  /* Splay the tree around KEY.  That will leave either the KEY
     itself, its predecessor, or its successor at the root.  */
  (void) splay_tree_splay (sp, key);

  /* */
  if (*sp->compvalue) {
    comparison = (*sp->compvalue)(sp->root->value, key);
  } else {
    comparison = (*sp->comp)(sp->root->key, key);
  }

  /* If the successor is at the root, just return it.  */
  if (comparison > 0) {
    return ((splay_tree_node)sp->root);
  }

  /* Otherwise, find the leftmost element of the right subtree.  */
  node = sp->root->right;

  if (node) {
    /* scan l */
    while (node->left)
      {
        node = node->left;
      }

  }

  return ((splay_tree_node)node);
}

/* Call FN, passing it the DATA, for every node in SP, following an
   in-order traversal.  If FN every returns a non-zero value, the
   iteration ceases immediately, and the value is returned.
   Otherwise, this function returns 0.
*/

int
splay_tree_foreach (splay_tree sp, splay_tree_foreach_fn fn, void *data)
{
  int value = 0;
  splay_tree_node node = (splay_tree_node)0;

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return (0);
  }

  /* no data */
  if (sp->root == (splay_tree_node)0) {
    return (0);
  }

  /* no function */
  if ((splay_tree_foreach_fn)0 == fn) {
    return (0);
  }

  /* lowest key */
  node = splay_tree_min (sp);

  /* no data */
  if (node == (splay_tree_node)0) {
    /* oke. */
    return 0;
  }

  /* oke status */
  value = 0;

  do
  {

      /* run function() */
      value = (*fn) (node, data);

      /* stop if function() error */
      if (value) {
        break;
      }

      /* next */
      node = splay_tree_successor (sp,(splay_tree_key)node->key);

      /* check */
      if (node == (splay_tree_node)0) {
        break;
      }

  } while (1);

  /* 0 if oke otherwise stop iter */
  return (value);
}

int
splay_tree_foreach2 (splay_tree sp, splay_tree_foreach_fn2 fn, void *data, void *data2)
{
  int value = 0;
  splay_tree_node node = (splay_tree_node)0;

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return (0);
  }

  /* no data */
  if (sp->root == (splay_tree_node)0) {
    return (0);
  }

  /* no function */
  if ((splay_tree_foreach_fn2)0 == fn) {
    return (0);
  }

  /* lowest key */
  node = splay_tree_min (sp);

  /* no data */
  if (node == (splay_tree_node)0) {
    /* oke. */
    return 0;
  }

  /* oke status */
  value = 0;

  do
  {

      /* run function() */
      value = (*fn) (node, data, data2);

      /* stop if function() error */
      if (value) {
        break;
      }

      /* next */
      node = splay_tree_successor (sp,(splay_tree_key)node->key);

      /* check */
      if (node == (splay_tree_node)0) {
        break;
      }

  } while (1);

  /* 0 if oke otherwise stop iter */
  return (value);
}

int
splay_tree_foreach3 (splay_tree sp, splay_tree_foreach_fn3 fn, void *data, void *data2, void *data3)
{
  int value = 0;
  splay_tree_node node = (splay_tree_node)0;

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return (0);
  }

  /* no data */
  if (sp->root == (splay_tree_node)0) {
    return (0);
  }

  /* no function */
  if ((splay_tree_foreach_fn3)0 == fn) {
    return (0);
  }

  /* lowest key */
  node = splay_tree_min (sp);

  /* no data */
  if (node == (splay_tree_node)0) {
    /* oke. */
    return 0;
  }

  /* oke status */
  value = 0;

  do
  {

      /* run function() */
      value = (*fn) (node, data, data2, data3);

      /* stop if function() error */
      if (value) {
        break;
      }

      /* next */
      node = splay_tree_successor (sp,(splay_tree_key)node->key);

      /* check */
      if (node == (splay_tree_node)0) {
        break;
      }

  } while (1);

  /* 0 if oke otherwise stop iter */
  return (value);
}

/* Call FN, passing it the DATA, for every node in SP, following an
   in-order traversal.  If FN every returns a non-zero value, the
   iteration ceases immediately, and the value is returned.
   Otherwise, this function returns 0
*/

int
splay_tree_foreach_backward (splay_tree sp, splay_tree_foreach_fn fn, void *data)
{
  int value = 0;
  splay_tree_node node = (splay_tree_node)0;

  /* <nil> splaytree */
  if ((splay_tree)0 == sp) {
    return (0);
  }

  /* no data */
  if (sp->root == (splay_tree_node)0) {
    return (0);
  }

  /* no function */
  if ((splay_tree_foreach_fn)0 == fn) {
    return (0);
  }

  /* with highest key */
  node = splay_tree_max (sp);

  /* no data */
  if (node == (splay_tree_node)0) {
    /* oke. */
    return 0;
  }

  /* oke status */
  value = 0;

  do
  {

      /* run function() */
      value = (*fn) (node, data);

      /* stop if function() error */
      if (value) {
        break;
      }

      /* earlier one */
      node = splay_tree_predecessor (sp,(splay_tree_key)node->key);

      /* check */
      if (node == (splay_tree_node)0) {
        break;
      }

  } while (1);

  /* 0 if oke otherwise stop iter */
  return (value);
}

/* move node after */
splay_tree_node splay_tree_move_after (splay_tree sp, splay_tree_node node, splay_tree_node after)
{
	splay_tree_node spnmax = (splay_tree_node)0;

	if (sp == (splay_tree)0) {
		return ((splay_tree_node)0);
	}

	if (node == (splay_tree_node)0) {
		return ((splay_tree_node)0);
	}

	if (after == (splay_tree_node)0) {
		return ((splay_tree_node)0);
	}

/* cannot move after last one because type of key is not known */

	/* Return the node in SP with the greatest key.  */
	spnmax = splay_tree_max (sp);

	if (spnmax) {}

/* tofix */

	return (splay_tree_node)0;
}

/* move node before */
splay_tree_node splay_tree_move_before (splay_tree sp, splay_tree_node node, splay_tree_node before)
{
	splay_tree_node spnmin = (splay_tree_node)0;

	if (sp == (splay_tree)0) {
		return ((splay_tree_node)0);
	}

	if (node == (splay_tree_node)0) {
		return ((splay_tree_node)0);
	}

	if (before == (splay_tree_node)0) {
		return ((splay_tree_node)0);
	}

	/* cannot move before first one because type of key is not known */

	/* Return the node in SP with the smallest key.  */
	spnmin = splay_tree_min (sp);

	/* or if at lowest sawp and renumber all */

	if (spnmin->key == before->key) {
		return ((splay_tree_node)0);
	}

	/* */
	spnmin->key = node->key;

	/* */
	before->key = node->key;

	/* */
	node->key = spnmin->key;

	/* */
	return ((splay_tree_node)node);
}

/* Splay-tree comparison function, treating the keys as ints.  */
int
splay_tree_compare_ints (splay_tree_key k1, splay_tree_key k2)
{
  if ((int) k1 < (int) k2) {
    return -1;
  } else if ((int) k1 > (int) k2) {
    return 1;
  } else {
    return 0;
  }
}

/* Splay-tree comparison function, treating the keys as pointers.  */

int
splay_tree_compare_pointers (splay_tree_key k1, splay_tree_key k2)
{
  if ((char *) k1 < (char *) k2) {
    return -1;
  } else if ((char *) k1 > (char *) k2) {
    return 1;
  } else {
    return 0;
  }
}


/* Comparison function for a splay tree in which the keys are strings.
   K1 and K2 have the dynamic type "const char *".  Returns <0, 0, or
   >0 to indicate whether K1 is less than, equal to, or greater than
   K2, respectively.  */

int
splay_tree_compare_strings (splay_tree_key k1, splay_tree_key k2)
{
  const char *s1 = (const char *)k1;
  const char *s2 = (const char *)k2;
  int ret = 0;

  if (s1 == (char *)0) {
    return (0);
  }

  if (s2 == (char *)0) {
    return (0);
  }

  /* check if same pointer */
  if (s1 == s2) {
    return 0;
  }

  ret = strcmp (s1, s2);

  return ret;
}

/* End. */
